<?php
session_start();
require_once "../connection.php";
	
	$username = 	$_POST['username'];
	$password = 	$_POST['password']; 
	$firstname = 	$_POST['firstname']; 
	$surname = 		$_POST['surname'];
	$email = 		$_POST['email'];
	$telephone = 	$_POST['telephone'];
	$imageurl = 	$_POST['imageurl'];
	$website = 		$_POST['website'];
	$usertype = 	$_POST['usertype'];
	
	/* testdata
	$username = 	"torsken"; 
	$firstname =    "Torstein";
	$surname = 		"Andreassen";
	$email = 		"dj_torsken@hotmail.com";
	$telephone = 	"90087475";
	$imageurl = 	"http://myimage.png";
	$website = 		"http://www.itsweb.no";
	$usertype = 	"developer";
	*/
	
	//$username','$firstname','$surname','$email','$telephone','$imgurl','$website','$usertype'	

    
	$sql = 'INSERT INTO user (username, password, firstname, surname, email, telephone, imageurl, website, usertype) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)';
	$sth = $db->prepare ($sql);
	$res = $sth->execute (array ($username,md5($password),$firstname,$surname,$email,$telephone,$imageurl,$website,$usertype));
	if ($res==1) {
	    $sql = 'SELECT id FROM user WHERE username=?';
        $sth = $db->prepare($sql);
        $sth->execute(array($username));
        $row = $sth->fetch();
        if($row) {
            echo json_encode (array ('ok'=>'OK'));
            $_SESSION['uname'] = $username;
            $_SESSION['usertype'] = $usertype;
            $_SESSION['uid'] = $row['id'];
        }

    } else {
	    echo json_encode (array ('message'=>'Brukernavnet finnes allerede i databasen.'));
    }

?>